dblogr.com/

Environmental Data Tutorial

An R tutorial on how to manipulate and plot environmental data


Introduction

The purpose of this document is to provide a few examples of how to extract environmental data in R and/or manipulate and plot various types of environmental data.


Data

The data for this vignette comes from two sources:

  1. Downloaded from EnvRtype and geosphere R packages.
  1. Manually collected data from various our lentil field trials in the AGILE project.

Prepare the data

# devtools::install_github("derekmichaelwright/agData")
library(agData)
# devtools::install_github('allogamous/EnvRtype')
library(EnvRtype)
# install.packages("geosphere")
library(geosphere)
#
myCaption <- "www.dblogr.com/ or derekmichaelwright.github.io/dblogr/ | Data: AGILE"
myEs <- c("Ro16", "Ro17", "Su16", "Su17", "Su18", "Us18",
          "In16", "In17", "Ba16", "Ba17", "Ne16", "Ne17",
          "Mo16", "Mo17", "Sp16", "Sp17", "It16", "It17")
myColors <- c("darkgreen", "darkred", "coral3", "deeppink3", "darkcyan",
              "burlywood4", "darkslateblue", "darkmagenta", "darkgoldenrod3")
#
ee <- read.csv("AGILE_Expts.csv") %>%
  mutate(Expt = factor(Expt, levels = myEs))
DT::datatable(ee)

EnvRtype and geosphere R packages

# Prep data
d1 <- get_weather(env.id = ee$Expt, lat = ee$Lat, lon = ee$Lon,
                  start.day = ee$Start, end.day = ee$End) %>%
  mutate(DayLength = daylength(lat = .$LAT, doy = .$YYYYMMDD))
#
write.csv(d1, "AGILE_R_EnvData.csv", row.names = F)
DT::datatable(d1)

Manual Data

# Prep data from 
d2 <- list() # Create Empty list
files <- list.files("AGILE_EnvData/")[grepl(".csv", list.files("AGILE_EnvData/"))]
for (i in 1:length(files)) { 
  d2[[i]] <- read.csv(paste0("AGILE_EnvData/", files[i]), stringsAsFactors = T)
  #colnames(d2[[i]])[1] <- "Location"
}
# Bind files together
d2 <- do.call(rbind, d2)
# Filter data
d2 <- d2 %>% filter(!is.na(DaysAfterPlanting))
#
# Prep data for plotting
d2 <- d2 %>% 
  mutate(Value = as.numeric(Value)) %>% 
  filter(!is.na(Value)) %>%
  mutate(Time = ifelse(is.na(Time) | Time == "", 
                       "12:00:00", as.character(Time)),
         Date = as.Date(Date),
         DateTime = as.POSIXct(paste(Date, Time), 
                               format = "%Y-%m-%d %H:%M:%OS"),
         #Location = factor(Location, levels = locs),
         LoggerID = factor(LoggerID, levels = unique(.$LoggerID)) )
#
write.csv(d2, "AGILE_My_EnvData.csv", row.names = F)
#
DT::datatable(d2)
# View table
d2 %>% as_tibble()
## # A tibble: 250,542 × 11
##    Expt  Location             Year Date       Time    LoggerID        Measurement Unit  Value DaysAfterPlanting DateTime           
##    <fct> <fct>               <int> <date>     <chr>   <fct>           <fct>       <fct> <dbl>             <int> <dttm>             
##  1 Ba16  Jessore, Bangladesh  2016 2016-11-15 6:21:00 Weather Station Day Length  Hours  10.9                 0 2016-11-15 06:21:00
##  2 Ba16  Jessore, Bangladesh  2016 2016-11-16 6:21:36 Weather Station Day Length  Hours  10.9                 1 2016-11-16 06:21:36
##  3 Ba16  Jessore, Bangladesh  2016 2016-11-17 6:22:12 Weather Station Day Length  Hours  10.9                 2 2016-11-17 06:22:12
##  4 Ba16  Jessore, Bangladesh  2016 2016-11-18 6:23:24 Weather Station Day Length  Hours  10.8                 3 2016-11-18 06:23:24
##  5 Ba16  Jessore, Bangladesh  2016 2016-11-19 6:24:00 Weather Station Day Length  Hours  10.8                 4 2016-11-19 06:24:00
##  6 Ba16  Jessore, Bangladesh  2016 2016-11-20 6:24:36 Weather Station Day Length  Hours  10.8                 5 2016-11-20 06:24:36
##  7 Ba16  Jessore, Bangladesh  2016 2016-11-21 6:25:12 Weather Station Day Length  Hours  10.8                 6 2016-11-21 06:25:12
##  8 Ba16  Jessore, Bangladesh  2016 2016-11-22 6:25:48 Weather Station Day Length  Hours  10.8                 7 2016-11-22 06:25:48
##  9 Ba16  Jessore, Bangladesh  2016 2016-11-23 6:26:24 Weather Station Day Length  Hours  10.8                 8 2016-11-23 06:26:24
## 10 Ba16  Jessore, Bangladesh  2016 2016-11-24 6:27:00 Weather Station Day Length  Hours  10.8                 9 2016-11-24 06:27:00
## # ℹ 250,532 more rows
# List experiments
d2 %>% distinct(Location, Year)
##               Location Year
## 1  Jessore, Bangladesh 2016
## 2  Jessore, Bangladesh 2017
## 3        Bhopal, India 2016
## 4        Bhopal, India 2017
## 5     Metaponto, Italy 2016
## 6     Metaponto, Italy 2017
## 7   Marchouch, Morocco 2016
## 8   Marchouch, Morocco 2017
## 9       Bardiya, Nepal 2016
## 10      Bardiya, Nepal 2017
## 11    Rosthern, Canada 2016
## 12    Rosthern, Canada 2017
## 13      Cordoba, Spain 2016
## 14      Cordoba, Spain 2017
## 15  Sutherland, Canada 2016
## 16  Sutherland, Canada 2017
## 17  Sutherland, Canada 2018
## 18  Central Ferry, USA 2018
# List Measurements from a single experiment
d2 %>% filter(Location == "Sutherland, Canada", Year == 2016) %>% 
  distinct(Measurement)
##               Measurement
## 1              Day Length
## 2             Temperature
## 3       Relative Humidity
## 4           Precipitation
## 5               Dew Point
## 6       Rain Accumulation
## 7         Solar Radiation
## 8     Barometric Pressure
## 9           Soil Moisture
## 10   Soil Temperature 5cm
## 11  Soil Temperature 10cm
## 12  Soil Temperature 20cm
## 13  Soil Temperature 50cm
## 14 Soil Temperature 100cm
# List data logger names from a single experiment
d2 %>% filter(Location == "Sutherland, Canada", Year == 2016) %>% 
  distinct(LoggerID)
##                 LoggerID
## 1        Weather Station
## 2       Weather Station1
## 3       Weather Station2
## 4       Weather Station3
## 5               IButton1
## 6               IButton2
## 7               IButton3
## 8                   Hobo
## 9 Kernen Weather Station

EnvRtype and geosphere R packages

By MacroEnv

# Prep data
xx <- d1 %>% 
  mutate(PRECTOT = ifelse(PRECTOT==0, NA, PRECTOT),
         DayLength_Rescaled = scales::rescale(DayLength, to = c(0, 40)))
# Plot
mp <- ggplot(xx, aes(x = YYYYMMDD)) +
  geom_hline(yintercept = c(10,30), alpha = 0.2, lty = 2) +
  geom_col(aes(y = PRECTOT, color = "Rain"), fill = "steelblue") +
  geom_line(aes(y = T2M, color = "Temp")) +
  geom_line(aes(y = DayLength_Rescaled, color = "Day Length")) +
  geom_ribbon(aes(ymin = T2M_MIN, ymax = T2M_MAX), 
              fill = "red", alpha = 0.25) +
  facet_wrap(. ~ env, ncol = 6, scales = "free_x") +
  scale_color_manual(values = c("red", "darkblue", "steelblue"), 
                     breaks = c("Temp", "Day Length", "Rain")) +
  scale_x_date(date_breaks = "month", date_labels = "%b") +
  scale_y_continuous(name = "\u00B0Celcius / mm Rain",
    sec.axis = sec_axis(~ . * (16.62-9.11) / (40-0) + 9.11, 
                        breaks = c(10, 12, 14, 16), name = "Hours") ) +
  coord_cartesian(ylim = c(0, 40)) +
  theme_agData(legend.position = "bottom") +
  labs(title = "Data from EnvRtype and geosphere R packages", 
       y = "Degree C / mm rain", x = NULL, caption = myCaption)
ggsave("envdata_1_01.png", mp, width = 12, height = 8)

By Trial

# Prep data
xx <- d1 %>% mutate(DayLength_Rescaled = scales::rescale(DayLength, to = c(0, 40)))
# Plot
mp <- ggplot(xx, aes(x = daysFromStart)) +
  geom_col(aes(y = PRECTOT), fill = "steelblue") +
  geom_line(aes(y = T2M)) +
  geom_line(aes(y = DayLength_Rescaled), color = "darkblue") +
  geom_ribbon(aes(ymin = T2M_MIN, ymax = T2M_MAX), 
              fill = alpha("red", 0.25), 
              color = alpha("red", 0.25) ) +
  facet_grid(env ~ ., scales = "free_x", space = "free_x") +
  scale_y_continuous(name = "\u00B0Celcius / mm Rain",
    sec.axis = sec_axis(~ . * (16.62-9.11) / (40-0) + 9.11, 
                        breaks = c(10, 12, 14, 16), name = "Hours") ) +
  coord_cartesian(ylim = c(0, 40)) +
  theme_agData() +
  labs(title = "Data from EnvRtype and geosphere R packages", 
       y = "Degree C / mm rain", x = NULL, caption = myCaption)
ggsave("envdata_1_02.png", mp, width = 6, height = 12)

Manaul Data

Summarize data

Calculcate min, mean and max for each measurement

xx <- d2 %>% 
  group_by(Location, Year, Date, Measurement) %>%
  summarise_at(vars(Value), funs(min, mean, max)) %>%
  ungroup()
str(xx)
## tibble [16,617 × 7] (S3: tbl_df/tbl/data.frame)
##  $ Location   : Factor w/ 9 levels "Jessore, Bangladesh",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ Year       : int [1:16617] 2016 2016 2016 2016 2016 2016 2016 2016 2016 2016 ...
##  $ Date       : Date[1:16617], format: "2016-11-15" "2016-11-15" "2016-11-15" "2016-11-15" ...
##  $ Measurement: Factor w/ 56 levels "Day Length","Dew Point",..: 1 2 3 14 15 16 17 1 2 3 ...
##  $ min        : num [1:16617] 10.9 20 15.5 0 74 ...
##  $ mean       : num [1:16617] 10.9 29 20.2 0 78.5 ...
##  $ max        : num [1:16617] 10.9 38 25 0 83 ...

All Data

Plotting Function

# Create Plotting function
envPlot <- function(xx) {
  ggplot(xx, aes(x = DateTime, y = Value, color = LoggerID)) +
  geom_line(alpha = 0.7) + 
  geom_point(alpha = 0.7, size = 0.5) +
  facet_grid(Measurement + Unit ~ ., scales = "free_y", switch = "y",
             labeller = label_wrap_gen(width = 10)) +
  theme_agData(strip.placement = "outside", legend.position = "bottom") +
  scale_x_datetime(date_labels = "%b %Y" , date_breaks = "1 month") +
  scale_color_manual(values = myColors) +
  labs(title = paste(unique(xx$Location), unique(xx$Year)), 
       y = NULL, x = NULL,
       caption = myCaption)
}

Sutherland, Canada 2016

mp <- envPlot(filter(d2, Location == "Sutherland, Canada", Year == 2016))
ggsave("envdata_2_01.png", mp, width = 12, height = 15)

Sutherland, Canada 2017

mp <- envPlot(filter(d2, Location == "Sutherland, Canada", Year == 2017))
ggsave("envdata_2_02.png", mp, width = 12, height = 11)

Sutherland, Canada 2018

mp <- envPlot(filter(d2, Location == "Sutherland, Canada", Year == 2018))
ggsave("envdata_2_03.png", mp, width = 12, height = 11)

Rosthern, Canada 2016

mp <- envPlot(filter(d2, Location == "Rosthern, Canada", Year == 2016))
ggsave("envdata_2_04.png", mp, width = 12, height = 10)

Rosthern, Canada 2017

mp <- envPlot(filter(d2, Location == "Rosthern, Canada", Year == 2017))
ggsave("envdata_2_05.png", mp, width = 12, height = 10)

Central Ferry, USA 2018

mp <- envPlot(filter(d2, Location == "Central Ferry, USA", Year == 2018))
ggsave("envdata_2_06.png", mp, width = 12, height = 10)

Bhopal, India 2016

mp <- envPlot(filter(d2, Location == "Bhopal, India", Year == 2016))
ggsave("envdata_2_07.png", mp, width = 12, height = 10)

Bhopal, India 2017

mp <- envPlot(filter(d2, Location == "Bhopal, India", Year == 2017))
ggsave("envdata_2_08.png", mp, width = 12, height = 5)

Jessore, Bangladesh 2016

mp <- envPlot(filter(d2, Location == "Jessore, Bangladesh", Year == 2016))
ggsave("envdata_2_09.png", mp, width = 12, height = 8)

Jessore, Bangladesh 2017

mp <- envPlot(filter(d2, Location == "Jessore, Bangladesh", Year == 2017))
ggsave("envdata_2_10.png", mp, width = 12, height = 4)

Bardiya, Nepal 2016

mp <- envPlot(filter(d2, Location == "Bardiya, Nepal", Year == 2016))
ggsave("envdata_2_11.png", mp, width = 12, height = 5)

Bardiya, Nepal 2017

mp <- envPlot(filter(d2, Location == "Bardiya, Nepal", Year == 2017))
ggsave("envdata_2_12.png", mp, width = 12, height = 4)

Marchouch, Morocco 2016

mp <- envPlot(filter(d2, Location == "Marchouch, Morocco", Year == 2016))
ggsave("envdata_2_13.png", mp, width = 12, height = 9)

Marchouch, Morocco 2017

mp <- envPlot(filter(d2, Location == "Marchouch, Morocco", Year == 2017))
ggsave("envdata_2_14.png", mp, width = 12, height = 5)

Cordoba, Spain 2016

mp <- envPlot(filter(d2, Location == "Cordoba, Spain", Year == 2016))
ggsave("envdata_2_15.png", mp, width = 12, height = 6)

Cordoba, Spain 2017

mp <- envPlot(filter(d2, Location == "Cordoba, Spain", Year == 2017))
ggsave("envdata_2_16.png", mp, width = 12, height = 6)

Metaponto, Italy 2016

mp <- envPlot(filter(d2, Location == "Metaponto, Italy", Year == 2016))
ggsave("envdata_2_17.png", mp, width = 12, height = 12)

Metaponto, Italy 2017

mp <- envPlot(filter(d2, Location == "Metaponto, Italy", Year == 2017))
ggsave("envdata_2_18.png", mp, width = 12, height = 11)

Compare Temperature Data

# Prep temperature data
x1 <- d1 %>% select(Expt=env, Date=YYYYMMDD, T_mean=T2M, T_min=T2M_MIN, T_max=T2M_MAX)
x2 <- d2 %>% 
  filter(Measurement == "Temperature") %>% 
  group_by(Expt, Measurement, Date) %>% 
  summarise_at(vars(Value), funs(min, mean, max)) %>%
  ungroup() %>%
  rename(T_mean=mean, T_min=min, T_max=max)
xx <- bind_rows(x1, x2)
# Plot graph
mp <- ggplot(x1, aes(x = Date, y = T_mean)) + 
  facet_wrap(Expt ~ ., ncol = 6, scales = "free_x") + 
  geom_line(color = "red") +
  geom_ribbon(aes(ymin = T_min, ymax = T_max), 
              fill = alpha("blue", 0.25), 
              color = alpha("blue", 0.25) ) +
  geom_line(data = x2, color = "blue") +
  geom_ribbon(data = x2, aes(ymin = T_min, ymax = T_max), 
              fill = alpha("red", 0.25), 
              color = alpha("red", 0.25) ) +
  scale_x_date(name = NULL, date_labels = "%b" , date_breaks = "1 month") +
  theme_agData() +
  labs(title = "Comparing Temperature Data", caption = myCaption)
ggsave("envdata_3_01.png", mp, width = 12, height = 8)

Temperature

Plot and calculate daily min, mean and max

# Filter data
xx <- d2 %>% filter(Location == "Sutherland, Canada", Year == 2016,
                    Measurement == "Temperature")
# Plot graph
mp <- ggplot(xx, aes(x = DateTime, y = Value, color = LoggerID)) +
  geom_line(alpha = 0.7) + 
  geom_point(alpha = 0.7, size = 0.5) +
  theme_agData(legend.position = "bottom") +
  scale_colour_manual(values = myColors) +
  scale_x_datetime(date_labels = "%b %Y" , date_breaks = "1 month") +
  labs(title = "Sutherland, Canada 2016", y = "Temperautre (C)", x = NULL,
       caption = myCaption)
ggsave("envdata_3_02.png", mp, width = 7, height = 5)

Plot temperatures for one month (July 2016)

mp <- mp + 
  scale_x_datetime(date_breaks = "1 month", date_minor_breaks = "1 day", 
                   date_labels = "%b %Y" , 
                   limits = as.POSIXct(c("2016-07-01","2016-08-01"))) +
  ylim(c(5,45))
ggsave("envdata_3_03.png", mp, width = 7, height = 5)

Calculate daily mean for each data logger

yy <- xx %>% 
  group_by(LoggerID, Date) %>%
  summarise(Value = mean(Value)) %>%
  spread(LoggerID, Value) %>% 
  mutate(Temp_Mean = rowMeans(select(.,-Date), na.rm = T))
str(yy)
## tibble [115 × 7] (S3: tbl_df/tbl/data.frame)
##  $ Date                  : Date[1:115], format: "2016-04-27" "2016-04-28" "2016-04-29" "2016-04-30" ...
##  $ Weather Station       : num [1:115] NA NA NA NA NA NA NA NA NA NA ...
##  $ IButton2              : num [1:115] NA NA 18.3 16.7 16.8 ...
##  $ IButton3              : num [1:115] NA NA 17.5 16.7 16.9 ...
##  $ Hobo                  : num [1:115] NA NA NA NA NA ...
##  $ Kernen Weather Station: num [1:115] 6.43 8.19 8.94 9.84 11.01 ...
##  $ Temp_Mean             : num [1:115] 6.43 8.19 14.93 14.4 14.91 ...

Calculate daily min, mean and max from specific data loggers

yy <- xx %>% 
  filter(LoggerID %in% c("Ibutton2","IButton3","Hobo")) %>% 
  group_by(Location, Date) %>% 
  summarise_at(vars(Value), funs(min, mean, max), na.rm = T) %>%
  rename(Temp_min = min, Temp_mean = mean, Temp_max = max)
str(yy)
## gropd_df [113 × 5] (S3: grouped_df/tbl_df/tbl/data.frame)
##  $ Location : Factor w/ 9 levels "Jessore, Bangladesh",..: 8 8 8 8 8 8 8 8 8 8 ...
##  $ Date     : Date[1:113], format: "2016-04-29" "2016-04-30" "2016-05-01" "2016-05-02" ...
##  $ Temp_min : num [1:113] 8.04 2.52 3.02 7.03 4.53 ...
##  $ Temp_mean: num [1:113] 17.5 16.7 16.9 22.2 21.8 ...
##  $ Temp_max : num [1:113] 27.1 35.5 35 41 46.5 ...
##  - attr(*, "groups")= tibble [1 × 2] (S3: tbl_df/tbl/data.frame)
##   ..$ Location: Factor w/ 9 levels "Jessore, Bangladesh",..: 8
##   ..$ .rows   : list<int> [1:1] 
##   .. ..$ : int [1:113] 1 2 3 4 5 6 7 8 9 10 ...
##   .. ..@ ptype: int(0) 
##   ..- attr(*, ".drop")= logi TRUE

Plot daily mean temperatures for each experiment

# Prep data
xx <- d2 %>% 
  filter(Measurement == "Temperature", Year != 2018) %>% 
  group_by(Location, Year, Expt, DaysAfterPlanting) %>% 
  summarise(Mean = mean(Value))
# Plot graph
mp <- ggplot(xx, aes(x = DaysAfterPlanting, y = Mean, color = Location)) + 
  stat_smooth(geom = "line", method = "loess", se = F, alpha = 0.7, size = 2) + 
  facet_grid(Year ~ .) +
  scale_color_manual(values = myColors) +
  theme_agData() +
  labs(title = "Temperature", 
       y = "Degrees Celsius", x = "Days After Planting",
       caption = myCaption)
ggsave("envdata_3_04.png", mp, width = 6, height = 4)

Day Lengths

# Prep data
xx <- d2 %>% 
  filter(Measurement == "Day Length", Year != 2018) %>% 
  group_by(Location, Year, Expt, DaysAfterPlanting) %>% 
  summarise(Mean = mean(Value) )
# Plot graph
mp <- ggplot(xx, aes(x = DaysAfterPlanting, y = Mean, color = Location)) + 
  geom_line(alpha = 0.7, size = 2) +
  facet_grid(Year ~ .) +
  scale_color_manual(values = myColors) +
  theme_agData() +
  labs(title = "Day Lengths",
       y = "Hours", x = "Days After Planting",
       caption = myCaption)
ggsave("envdata_3_05.png", mp, width = 6, height = 4)

Precipitation

# Prep data
xx <- d2 %>% 
  filter(Measurement == "Precipitation") %>%
  group_by(Expt, Location, Year) %>%
  summarise(Rainfall = sum(Value)) %>%
  arrange(Rainfall) %>%
  ungroup() %>%
  mutate(Expt = factor(Expt, levels = Expt),
         Year = factor(Year))
# Plot graph
mp <- ggplot(xx, aes(x = Location, y = Rainfall, fill = Year)) + 
  geom_col(alpha = 0.7, color = "black", lwd = 0.3,
           position = position_dodge2(preserve = "single")) + 
  scale_fill_manual(values = myColors) +
  theme_agData() +
  coord_flip() +
  labs(title = "Total Precipitation",
       caption = myCaption)
ggsave("envdata_3_06.png", mp, width = 6, height = 4)

# Prep data
xx <- d2 %>% 
  filter(Measurement == "Precipitation", Year == 2016) %>% 
  group_by(Location, Year, Expt, Date, DaysAfterPlanting) %>% 
  summarise(Value = sum(Value))
# Plot graph
mp <- ggplot(xx, aes(x = DaysAfterPlanting, y = Value)) + 
  geom_col(fill = "steelblue") +
  facet_wrap(Location~., ncol = 1) +
  theme_agData() +
  labs(title = "Precipitation", y = "mm", x = "Days After Planting",
       caption = myCaption)
ggsave("envdata_3_07.png", mp, width = 6, height = 8)

Temperature, Day Length & Rainfall

Temperature and Daylength will require separate axes so we will have to rescale the data.

\[y_{scaled}=(y_{2i}-min(y_2))*\frac{max(y_1)-min(y_1)}{max(y_2)-min(y_2)}\]

\[y_{2i}=y_{scaled}*\frac{max(y_2)-min(y_2)}{max(y_1)-min(y_1)}+min(y_2)\]

e.g.,

\[12=(11.333-9.11)*\frac{(40-0)}{16.52-9.11})\]

\[11.333=12*\frac{16.52-9.11}{40-0}+9.11\]

# Prep rainfall data
myLocs <- c("Sutherland, Canada", "Metaponto, Italy", "Bhopal, India")
xR <- d2 %>% 
  filter(Location %in% myLocs, Year == 2017, 
         Measurement == "Precipitation", Value > 0) %>%
  group_by(Expt, Date, DaysAfterPlanting) %>% 
  summarise(Value = sum(Value))
# Prep temperature and daylength data
xx <- d2 %>% 
  filter(Location %in% myLocs, Year == 2017,
         Measurement %in% c("Temperature", "Day Length")) %>% 
  group_by(Expt, Measurement, Date) %>% 
  summarise_at(vars(Value), funs(min, mean, max)) %>%
  ungroup() %>%
  gather(Calc, Value, min, mean, max) %>%
  mutate(Measurement = plyr::mapvalues(Measurement, 
           c("Temperature", "Day Length"), c("Temp", "DayLen")),
         Measurement = paste(Measurement, Calc, sep="_")) %>%
  select(-Calc) %>%
  filter(!Measurement %in% c("DayLen_min", "DayLen_max")) %>%
  spread(Measurement, Value) %>%
  mutate(DayLen_Rescaled = scales::rescale(DayLen_mean, to = c(0, 45)))
# Print
str(xx)
## tibble [450 × 7] (S3: tbl_df/tbl/data.frame)
##  $ Expt           : Factor w/ 18 levels "Ba16","Ba17",..: 4 4 4 4 4 4 4 4 4 4 ...
##  $ Date           : Date[1:450], format: "2017-11-09" "2017-11-10" "2017-11-11" "2017-11-12" ...
##  $ DayLen_mean    : num [1:450] 11 11 11 10.9 10.9 ...
##  $ Temp_max       : num [1:450] 30.5 29.6 27.3 28.1 28.9 29.5 30.5 28.4 26.7 25.3 ...
##  $ Temp_mean      : num [1:450] 26.6 25.6 24 23.6 24.8 ...
##  $ Temp_min       : num [1:450] 22.6 21.7 20.7 19.2 20.6 19.3 21.6 20.3 20.5 19.4 ...
##  $ DayLen_Rescaled: num [1:450] 11.5 11.4 11.3 11.2 11.1 ...
# Collect values for rescaling seconday axis
range(xx$DayLen_mean)
## [1]  9.11 16.52
# Plot graph
mp <- ggplot(xx, aes(x = Date, y = Temp_mean)) + 
  facet_wrap(Expt ~ ., ncol = 1, scales = "free_x") + 
  geom_line(color = "red") +
  geom_line(aes(y = DayLen_Rescaled), color = "darkblue") +
  geom_col(data = xR, aes(y = Value), fill = "steelblue") +
  geom_ribbon(aes(ymin = Temp_min, ymax = Temp_max), 
              fill = alpha("red", 0.25), 
              color = alpha("red", 0.25) ) +
  scale_x_date(name = NULL, date_labels = "%b" , date_breaks = "1 month") +
  scale_y_continuous(name = "\u00B0Celcius / mm Rain",
    sec.axis = sec_axis(~ . * (16.62-9.11) / (45-0) + 9.11, 
                        breaks = c(10, 12, 14, 16), name = "Hours") ) +
  coord_cartesian(ylim=c(0, 45)) +
  theme_agData() +
  labs(caption = myCaption)
ggsave("envdata_3_08.png", mp, width = 6, height = 5.5)

Soil Temperature

# Prep data
mm <- c("Soil Temperature 5cm",  "Soil Temperature 10cm",
        "Soil Temperature 20cm", "Soil Temperature 50cm",
        "Soil Temperature 100cm")
xx <- d2 %>% 
  filter(Location == "Sutherland, Canada", Year == 2016, 
         Measurement %in% mm) %>%
  mutate(Measurement = factor(Measurement, levels = mm),
         Measurement = plyr::mapvalues(Measurement, mm, 
           c("5cm","10cm","20cm","50cm","100cm")))
# Plot
mp <- ggplot(xx, aes(x = DateTime, y = Value, color = Measurement)) +
  geom_line(alpha = 0.7) +
  scale_color_manual(values = myColors) +
  theme_agData() +
  labs(title = "Soil Temperatures: Sutherland, Canada 2016",
       y = NULL, x = NULL,
       caption = myCaption)
ggsave("envdata_3_09.png", mp, width = 6, height = 4)

Extra EnvRtype Examples

envdata_4_01.png
envdata_4_01.png
# Prep data
xx <- read.csv("My_Expts_1.csv")
DT::datatable(xx)
#
ee <- get_weather(env.id = xx$Expt, lat = xx$Lat, lon = xx$Lon,
                  start.day = xx$Start, end.day = xx$End) %>%
  mutate(DayLength = daylength(lat = .$LAT, doy = .$YYYYMMDD) )
# Plot
mp <- ggplot(ee, aes(x = YYYYMMDD)) +
  geom_line(aes(y = T2M)) +
  geom_line(aes(y = DayLength), color = "darkblue") +
  geom_ribbon(aes(ymin = T2M_MIN, ymax = T2M_MAX), 
              fill = alpha("red", 0.25), 
              color = alpha("red", 0.25) ) +
  geom_col(aes(y = PRECTOT), fill = "steelblue") +
  scale_x_date(date_breaks = "month", date_labels = "%b") +
  facet_grid(. ~ env, scales = "free_x", space = "free_x") +
  theme_agData()
ggsave("envdata_4_01.png", mp, width = 15, height = 4)
write.csv(ee, "envdata_myExpts_1.csv", row.names = F)

envdata_4_02.png
envdata_4_02.png
# Prep data
xx <- read.csv("My_Expts_2.csv")
DT::datatable(xx)
#
ee <- get_weather(env.id = xx$Expt, lat = xx$Lat, lon = xx$Lon,
                  start.day = xx$Start, end.day = xx$End) %>%
  mutate(DayLength = daylength(lat = .$LAT, doy = .$YYYYMMDD) )
# Plot
mp <- ggplot(ee, aes(x = YYYYMMDD)) +
  geom_line(aes(y = T2M)) +
  geom_line(aes(y = DayLength), color = "darkblue") +
  geom_ribbon(aes(ymin = T2M_MIN, ymax = T2M_MAX), 
              fill = alpha("red", 0.25), 
              color = alpha("red", 0.25) ) +
  geom_col(aes(y = PRECTOT), fill = "steelblue") +
  scale_x_date(date_breaks = "month", date_labels = "%b") +
  facet_grid(. ~ env, scales = "free_x", space = "free_x") +
  theme_agData()
ggsave("envdata_4_02.png", mp, width = 15, height = 4)
write.csv(ee, "envdata_myExpts_2.csv", row.names = F)


dblogr.com/

© Derek Michael Wright